package logic;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;

import model.PredictionEnergyProfile;
import model.ProbabilityEnergyProfile;
import model.SimpleEnergyProfile;

import com.csvreader.CsvReader;

public class EnergyProfilerFile {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println("EnergyProfilerFile");
		
		if (args.length != 1) {
			System.out.println("requires input csv file.");
		}
		
		//run the profiler!
		else {
			EnergyProfilerFile epf = new EnergyProfilerFile();
			//ArrayList<ProbabilityEnergyProfile> problist = 
			epf.profile(args[0]);
			
		}
	}
	
	public static void printTopX(ArrayList<ProbabilityEnergyProfile> problist, int num) {
		for (ProbabilityEnergyProfile prob: problist) {
			//for(int i=0;i<prob.getProbabilityList().size();i++)
			int i = prob.getProbabilityList().size() - 1;
			for (int offset=0;offset<num;offset++) {
				System.out.println(prob.getId()+" "
						+prob.getProbabilityList().get(i-offset).getUsageType()+": "
						+prob.getProbabilityList().get(i-offset).getProbability()+" "
						+prob.getProbabilityList().get(i-offset).getZ());
			}
		}
	}
	
	public void profile(String source) {
		
		//read the input file
		ArrayList<SimpleEnergyProfile> simplelist = readProfileFile(source);
		
		//load data
		//DataInitializer di = new DataInitializer();
		//ArrayList<EnergyProfile> iesoData = di.initializeData();
		
		//process prediction list
		String preddir = "src\\resources\\weka\\";
		String predsource;
		/*= preddir+"weka_nomonth_training.csv";
		System.out.println(predsource);
		prepAndRunAnalysis(predsource,simplelist);
		ArrayList<PredictionEnergyProfile> predlist = processPredictionList(predsource);
		//run analysis
		ArrayList<ProbabilityEnergyProfile> problist = runAnalysis(predlist,simplelist);
		printTopX(problist, 3);*/
		
		
		predsource = preddir+"weka_industryrg_month_cross.csv";
		System.out.println(predsource);
		prepAndRunAnalysis(predsource,simplelist);
		
		predsource = preddir+"weka_industrycg_month_cross.csv";
		System.out.println(predsource);
		prepAndRunAnalysis(predsource,simplelist);

		predsource = preddir+"weka_industrysi_month_cross.csv";
		System.out.println(predsource);
		prepAndRunAnalysis(predsource,simplelist);
		
		predsource = preddir+"weka_industryrg_nomonth_cross.csv";
		System.out.println(predsource);
		prepAndRunAnalysis(predsource,simplelist);
		
		predsource = preddir+"weka_industrycg_nomonth_cross.csv";
		System.out.println(predsource);
		prepAndRunAnalysis(predsource,simplelist);

		predsource = preddir+"weka_industrysi_nomonth_cross.csv";
		System.out.println(predsource);
		prepAndRunAnalysis(predsource,simplelist);
		
		predsource = preddir+"weka_usagetype_month_cross.csv";
		System.out.println(predsource);
		prepAndRunAnalysis(predsource,simplelist);
		
		predsource = preddir+"weka_usagetype_nomonth_cross.csv";
		System.out.println(predsource);
		prepAndRunAnalysis(predsource,simplelist);
		
		predsource = preddir+"weka_industry_nousagetype_month_cross.csv";
		System.out.println(predsource);
		prepAndRunAnalysisIndustry(predsource,simplelist);
		
		predsource = preddir+"weka_industry_nousagetype_nomonth_cross.csv";
		System.out.println(predsource);
		prepAndRunAnalysisIndustry(predsource,simplelist);
		
		System.out.println("Electricity Intensity");
		prepAndRunAnalysisElectricityIntensity(simplelist);
		//is date supplied? minimize search
		
		//return problist;
		
	}
	public void prepAndRunAnalysisElectricityIntensity(ArrayList<SimpleEnergyProfile> simplelist) {
		
		//run analysis
		ArrayList<ProbabilityEnergyProfile> problist = runAnalysisEnergyIntensity(simplelist);
		
		System.out.println();
		printTopX(problist, 3);
		
	}
	public void prepAndRunAnalysisIndustry(String predsource, ArrayList<SimpleEnergyProfile> simplelist) {
		ArrayList<PredictionEnergyProfile> predlist = processPredictionList(predsource);
		System.out.println(predlist.size());
		//run analysis
		ArrayList<ProbabilityEnergyProfile> problist = runAnalysis(predlist,simplelist);
		for (PredictionEnergyProfile pred: predlist) {
			System.out.print(pred.getUsageType() +" ");
		}
		System.out.println();

		for (ProbabilityEnergyProfile prob: problist) {
			if (prob.getProbabilityList().get(1).getProbability()<0.5)
				System.out.println(prob.getId()+" "
						+"Industrial"+": "
						+(1 - prob.getProbabilityList().get(1).getProbability()
								-prob.getProbabilityList().get(0).getProbability()));
			else
				System.out.println(prob.getId()+" "
						+prob.getProbabilityList().get(1).getUsageType()+": "
						+prob.getProbabilityList().get(1).getProbability());
		}
		
		predlist =null;
	}
	public void prepAndRunAnalysis(String predsource, ArrayList<SimpleEnergyProfile> simplelist) {
		ArrayList<PredictionEnergyProfile> predlist = processPredictionList(predsource);
		System.out.println(predlist.size());
		//run analysis
		ArrayList<ProbabilityEnergyProfile> problist = runAnalysis(predlist,simplelist);
		for (PredictionEnergyProfile pred: predlist) {
			System.out.print(pred.getUsageType() +" ");
		}
		System.out.println();
		printTopX(problist, 3);
		predlist =null;
	}
	
	public ArrayList<SimpleEnergyProfile> readProfileFile(String source) {
		//read the input file
		CsvReader reader;
		
		try {
			reader = new CsvReader(source);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return null;
		}
		try {
			reader.readHeaders();
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
		//String useHeader = reader.getHeader(0);
		ArrayList<SimpleEnergyProfile> simplelist = new ArrayList<SimpleEnergyProfile>();
		try {
			while (reader.readRecord())
			{
				SimpleEnergyProfile sep = new SimpleEnergyProfile();
				sep.setId(Integer.parseInt(reader.get("Id")));
				sep.setIndustry(reader.get("Industry"));
				sep.setUsageType(reader.get("Usage Type"));
				sep.setAreasqft(Integer.parseInt(reader.get("sqft")));
				sep.setAnnualEnergy(Integer.parseInt(reader.get("Annual Energy")));
				sep.setMonth(reader.get("Month"));
				sep.setDay(reader.get("Day"));
				
				double[] doubleArray = new double[24];
				for (int i=0; i<24; i++) {
					doubleArray[i] = 
					Double.parseDouble(reader.get(String.valueOf(i+1)));
				}
				sep.setEnergyProfile(doubleArray);
				sep.normalizeData(); // new
				simplelist.add(sep);
				System.out.println(sep);
			}
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
		return simplelist;
	}
	
	public ArrayList<PredictionEnergyProfile> processPredictionList(String predsource) {
		DataInitializer di = new DataInitializer();
		try {
			return di.processLogisticRegression(predsource);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public ArrayList<ProbabilityEnergyProfile> runAnalysis(ArrayList<PredictionEnergyProfile> predlist, ArrayList<SimpleEnergyProfile> simplelist){
		//run analysis
		DataMatcher dm = new DataMatcher();
		return dm.createProbabilityMatcher(predlist, simplelist);
		
	}
	public ArrayList<ProbabilityEnergyProfile> runAnalysisEnergyIntensity(ArrayList<SimpleEnergyProfile> simplelist){
		//run analysis
		DataMatcher dm = new DataMatcher();
		return dm.createProbabilityMatcherEnergyIntensity( simplelist);
		
	}
}
